//
//  File.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/5/18.
//

import Combine

final class TuyaPwdLoginFlow {
    
    private let cdGetUserInfoStep: CDGetUserLoginInfoStep    // 第一步，调用凯度云服务获取用户信息
    private let tuyaLoginPwdStep: TuyaLoginPwdStep           // 第二步，涂鸦账户密码登录
    
    init() {
        cdGetUserInfoStep = CDGetUserLoginInfoStep()
        tuyaLoginPwdStep = TuyaLoginPwdStep()
    }
    
    func start() -> AnyPublisher<Void, Error> {
        cdGetUserInfoStep.execute(.init())
            .flatMap { [weak self] userInfo -> AnyPublisher<Void, Error> in
                return self?.tuyaLoginPwdStep.execute(.init(account:  userInfo.userName, password: userInfo.password)) ?? Fail(error: FlowError.stepMissing).eraseToAnyPublisher()
            }
            .eraseToAnyPublisher()
    }
}
